require "common/Define"
require "framework/BaseController"
require "FairyGUI"

TypingEffectMain = BaseController:New({mControllerName = "TypingEffectMain"});

function TypingEffectMain.Main()
	-- body
	TypingEffectMain:Awake();
end

function TypingEffectMain:Awake()
	Application.targetFrameRate = 60;
	Stage.inst.onKeyDown:Add(self.OnKeyDown, self);

	-- attack pacakge, package in resource or assetbundle
	local packageName = "UI/TypingEffect";
	UIPackage.AddPackage(packageName);

	local componentName = "Main";
	self.mainComponent = UIPackage.CreateObject("TypingEffect", componentName).asCom;
	GRoot.inst:AddChild( self.mainComponent );

	--local go = GameObject();
	--local panel = go:AddComponent(typeof(UIPanel));
	--panel.container:AddChild(self.mainComponent);
	--GRoot.inst:AddChild( panel );

	-- self.mainComponent:SetSize(GRoot.inst.width, GRoot.inst.height);
	-- self.mainComponent:AddRelation(GRoot.inst, RelationType.Size);
	GRoot.inst:SetContentScaleFactor(1134, 640, FairyGUI.UIContentScaler.ScreenMatchMode.MatchWidthOrHeight);

	self:OnViewCreated(self.mainComponent);
end

function TypingEffectMain:InitView()
	self._te1 = FairyGUI.TypingEffect( self.mainComponent:GetChild("n2").asTextField );
	self._te1:Start();
	FairyGUI.Timers.inst:StartCoroutine( self._te1:Print(0.050) );

	self._te2 = FairyGUI.TypingEffect( self.mainComponent:GetChild("n3").asTextField );
	self._te2:Start();
	FairyGUI.Timers.inst:Add(0.050, 0, self.PrintText);
end

function TypingEffectMain.PrintText(param)
	if not TypingEffectMain._te2:Print() then
		FairyGUI.Timers.inst:Remove(TypingEffectMain.PrintText);
	end
end

function TypingEffectMain:OnKeyDown(context)
 	-- body
 	if (context.inputEvent.keyCode == KeyCode.Escape) then
		print "Exit Game!"
		Application.Quit();
	end
end
